package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.CertificateChangeLog;
import com.lxw.nonghe.service.CertificateChangeLogService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 慢病证变更记录表 前端控制器
 * </p>
 *
 * @author lxw
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/certificateChangeLog")
@RequiresPermission("certificateLog:manage")
public class CertificateChangeLogController {

    @Autowired
    private CertificateChangeLogService changeLogService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addChangeLog(@RequestBody CertificateChangeLog changeLog) {
        try {
            // 初始化变更时间
            changeLog.setChangeTime(new Date());

            boolean result = changeLogService.save(changeLog);
            if (result) {
                logUtils.recordLog("慢病证变更记录管理", "新增记录", "成功",
                        "证书ID:" + changeLog.getCertificateId() + ", 变更类型:" + changeLog.getChangeType());
                return Result.success("慢病证变更记录新增成功", true);
            } else {
                logUtils.recordLog("慢病证变更记录管理", "新增记录", "失败", "保存数据库失败");
                return Result.error("慢病证变更记录新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证变更记录管理", "新增记录", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteChangeLog(@PathVariable Integer id) {
        try {
            CertificateChangeLog changeLog = changeLogService.getById(id);
            boolean result = changeLogService.removeById(id);

            if (result) {
                logUtils.recordLog("慢病证变更记录管理", "删除记录", "成功",
                        "记录ID:" + id + ", 证书ID:" + (changeLog != null ? changeLog.getCertificateId() : "未知"));
                return Result.success("慢病证变更记录删除成功", true);
            } else {
                logUtils.recordLog("慢病证变更记录管理", "删除记录", "失败", "记录ID:" + id + "不存在");
                return Result.error("慢病证变更记录删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证变更记录管理", "删除记录", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateChangeLog(@RequestBody CertificateChangeLog changeLog) {
        try {
            boolean result = changeLogService.updateById(changeLog);
            if (result) {
                logUtils.recordLog("慢病证变更记录管理", "更新记录", "成功",
                        "记录ID:" + changeLog.getLogId());
                return Result.success("慢病证变更记录更新成功", true);
            } else {
                logUtils.recordLog("慢病证变更记录管理", "更新记录", "失败",
                        "记录ID:" + changeLog.getLogId() + "不存在");
                return Result.error("慢病证变更记录更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证变更记录管理", "更新记录", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<CertificateChangeLog> getChangeLogById(@PathVariable Integer id) {
        try {
            CertificateChangeLog changeLog = changeLogService.getById(id);
            if (changeLog != null) {
                return Result.success("慢病证变更记录查询成功", changeLog);
            } else {
                logUtils.recordLog("慢病证变更记录管理", "查询记录详情", "失败", "记录ID:" + id + "不存在");
                return Result.error("未找到该慢病证变更记录");
            }
        } catch (Exception e) {
            logUtils.recordLog("慢病证变更记录管理", "查询记录详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<CertificateChangeLog>> getChangeLogPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer certificateId,
            @RequestParam(required = false) String changeType,
            @RequestParam(required = false) String operatorName) {
        try {
            Page<CertificateChangeLog> page = changeLogService.getChangeLogPage(
                    current, size, certificateId, changeType, operatorName);
            return Result.success("慢病证变更记录分页查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("慢病证变更记录管理", "分页查询记录", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }
    // 新增条件查询方法
    @PostMapping("/query")
    public Result<Page<CertificateChangeLog>> queryChangeLogs(
            @RequestBody CertificateChangeLog queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<CertificateChangeLog> page = changeLogService.queryChangeLogs(
                    queryParam, current, size);
            return Result.success("慢病证变更记录条件查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("慢病证变更记录管理", "条件查询记录", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
